﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls
 * 唯一标识：65f4aaaf-e313-44a4-9a48-2c6272b04c59
 * 文件名：Button
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/25 15:24:25
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
///
/// </summary>
public sealed class Button : ControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Button()
    {
        ExcuteType = ButtonExcuteType.Custom;
    }

    public override string Id { get; set; }

    /// <summary>
    /// 按钮类型
    /// </summary>
    internal ButtonType Type { get; set; }

    /// <summary>
    /// 按钮执行方式
    /// </summary>
    internal ButtonExcuteType ExcuteType { get; set; }

    

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param param = new Param();

        if (HasClick)
        {
            StringBuilder sb2 = new StringBuilder();

            switch (ExcuteType)
            {
                case ButtonExcuteType.Submit:
                    {
                        sb2.AppendLine($"request.post('/api/click/{MethodName}', form)");
                        sb2.AppendLine($".then(response => {{");
                        sb2.AppendLine($"{App.Message("成功")}");
                        sb2.AppendLine($"}})");
                        sb2.AppendLine($".catch(error => {{");
                        sb2.AppendLine($"this.$message({{ message: error.response.data,type: 'error'}})");
                        sb2.AppendLine($"}});");

                    }
                    break;
                case ButtonExcuteType.Custom:
                    {
                        sb2.AppendLine($"const requestBody = {{");
                        sb2.AppendLine(RequestBody);
                        sb2.AppendLine($"}};");
                        sb2.AppendLine($"request.post('/api/click/{MethodName}', requestBody)");
                        sb2.AppendLine($".then(response => {{");
                        sb2.AppendLine($"{App.Message("成功")}");
                        sb2.AppendLine($"}})");
                        sb2.AppendLine($".catch(error => {{");
                        sb2.AppendLine($"{App.Message("error.response.data", "error")}");
                        sb2.AppendLine($"}});");
                    }
                    break;
            }

            param.Add($"@click=\"()=>{{  {sb2.ToString()}  }}\"");
        }

        sb.AppendLine($"<el-button type=\"{Type.GetDescription()}\" {param} >{Title}</el-button>");

        return sb.ToString();
    }

    #region 点击事件监听

    /// <summary>
    /// 是否配置了按钮点击
    /// </summary>
    internal bool HasClick { get; set; }

    /// <summary>
    /// 按钮编号
    /// </summary>
    internal string MethodName { get; set; }

    

    /// <summary>
    /// 请求的内容
    /// </summary>
    internal string RequestBody { get; set; }


    public override string Title { get; set; }

    /// <summary>
    /// 设置点击事件
    /// </summary>
    /// <param name="methodName">触发方法名称</param>
    /// <param name="requestBody">请求内容</param>
    /// <returns></returns>
    public Button SetClick(string methodName, string requestBody)
    {
        HasClick = true;
        MethodName = methodName;

        if (string.IsNullOrEmpty(requestBody))
            throw new ArgumentNullException("requestBody参数不能为空!");

        RequestBody = requestBody;
        return this;
    }

    /// <summary>
    /// 将按钮设置为提交
    /// </summary>
    /// <param name="methodName"></param>
    /// <returns></returns>
    public Button SetSubmit(string methodName)
    {
        ExcuteType = ButtonExcuteType.Submit;
        HasClick = true;
        MethodName = methodName;

        return this;
    }


    /// <summary>
    /// 设置自定义按钮
    /// </summary>
    /// <param name="methodName"></param>
    /// <returns></returns>
    public Button SetCustomBtn(string methodName)
    {
        ExcuteType = ButtonExcuteType.Custom;
        HasClick = true;
        MethodName = methodName;

        return this;
    }

    #endregion
}

public static partial class App
{
    /// <summary>
    /// 创建按钮
    /// </summary>
    /// <param name="name">按钮名称</param>
    /// <param name="type">按钮类型</param>
    /// <returns></returns>
    public static Button CreateButton(string name, ButtonType type)
    {
        return new Button
        {
            Title = name,
            Id = GenerateId(),
            Type = type,
        };
    }
}

/// <summary>
/// 按钮类型
/// </summary>
public enum ButtonType
{
    /// <summary>
    /// 默认
    /// </summary>
    [Description("")]
    Default,
    /// <summary>
    /// 
    /// </summary>
    [Description("primary")]
    Primary,
    /// <summary>
    /// 
    /// </summary>
    [Description("success")]
    Success,
    /// <summary>
    /// 
    /// </summary>
    [Description("info")]
    Info,
    /// <summary>
    /// 
    /// </summary>
    [Description("warning")]
    Warning,
    /// <summary>
    /// 
    /// </summary>
    [Description("danger")]
    Danger
}

/// <summary>
/// 按钮执行方式
/// </summary>
public enum ButtonExcuteType
{
    /// <summary>
    /// 执行提交
    /// </summary>
    [Description("submit")]
    Submit,
    /// <summary>
    /// 关闭当前页
    /// </summary>
    [Description("closePage")]
    ClosePage,
    /// <summary>
    /// 执行自定义调用
    /// </summary>
    [Description("custom")]
    Custom
}